package cn.edu.ustc.xrovers.daoImpl;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import cn.edu.ustc.xrovers.dao.CompanyInfoDao;
import cn.edu.ustc.xrovers.model.BalanceSheet;
import cn.edu.ustc.xrovers.model.CashFlowStatement;
import cn.edu.ustc.xrovers.model.Company;
import cn.edu.ustc.xrovers.model.CompanyRole;
import cn.edu.ustc.xrovers.model.GuaranteeWay;
import cn.edu.ustc.xrovers.model.IncomeStatement;
import cn.edu.ustc.xrovers.model.Loan;
import cn.edu.ustc.xrovers.model.LoanStatus;
import cn.edu.ustc.xrovers.model.Manager;
import cn.edu.ustc.xrovers.model.NonFinancialInformation;

/**
 * 处理企业信息的DAO实现类
 * @author paddy
 *
 */
public class CompanyInfoDaoImpl extends BaseDao implements CompanyInfoDao {

	/**
	 * 保存企业基本信息
	 * @param company Company持久化对象
	 * @param role 企业所属的角色类型
	 * @return 保存成功返回true，否则返回false
	 */
	public boolean saveBaseInfo(final Company company,final String role) {
		try{
			Object isExisted=ht.execute(new HibernateCallback<Object>() {
				public Object doInHibernate(Session session)
				throws HibernateException, SQLException {
					Query query=session.createQuery("from Company c where c.companyId=:companyId");
					query.setParameter("companyId", company.getCompanyId());
					return query.uniqueResult();
				}
			});
			if(isExisted!=null)
				return false;
			else{
				CompanyRole cr=(CompanyRole) ht.execute(new HibernateCallback<Object>() {
					public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
						Query query=session.createQuery("from CompanyRole cr where cr.role=:role");
						query.setParameter("role", role);
						return (CompanyRole)query.uniqueResult();
					}
				});
				company.setRole(cr);
				ht.save(company);
				return true;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 保存企业财务信息
	 * @param bs BalanceSheet资产负债持久化对象
	 * @param cfs CashFlowStatement现金流量持久化对象
	 * @param is IncomeStatement利润持久化对象
	 * @param company Company持久化对象
	 * @return 保存成功返回true，否则返回false
	 */
	public boolean saveFinInfo(BalanceSheet bs, CashFlowStatement cfs, IncomeStatement is, Company company) {
		try{
			bs.setCompany(company);
			ht.save(bs);
			cfs.setCompany(company);
			ht.save(cfs);
			is.setCompany(company);
			ht.save(is);
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * 保存企业非财务信息
	 * @param nfi NonFinancialInformation持久化类
	 * @param company Company持久化类
	 * @return 保存成功返回true，否则返回false
	 */
	public boolean saveNonFinInfo(NonFinancialInformation nfi, Company company) {
		try{
			nfi.setCompany(company);
			ht.save(nfi);
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * 保存贷款基本信息
	 * 金额，担保方式，日期
	 * @param loan Loan实体类对象，贷款基本信息
	 * @param company Company实体类对象，贷款企业的基本信息
	 * @return 保存成功返回true，否则返回false
	 */
	public boolean saveLoanInfo(Loan loan, final String guaWay, Company company, Manager manager) {
		try{
			GuaranteeWay guaranteeWay=(GuaranteeWay)ht.execute(new HibernateCallback<Object>() {
				public Object doInHibernate(Session session)
				throws HibernateException, SQLException {
					Query query=session.createQuery("from GuaranteeWay gw where gw.guaWay=:guaway");
					query.setParameter("guaway", guaWay);
					return query.uniqueResult();
				}
			});
			LoanStatus ls=(LoanStatus)ht.execute(new HibernateCallback<Object>() {
				public Object doInHibernate(Session session)
				throws HibernateException, SQLException {
					Query query=session.createQuery("from LoanStatus ls where ls.lstatus='unapproved'"); 
					return query.uniqueResult();
				}
			});
			loan.setGuaWay(guaranteeWay);
			loan.setStatus(ls);
			loan.setCompany(company);
			loan.setManager(manager);
			loan.setIsAnalyzed(false);
			ht.save(loan);
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * 根据贷款编号查询相应报表
	 * @param loanId 贷款编号
	 * @param reportname 报表名称
	 * @return 代表报表信息的实体类对象
	 */
	public Object findReportInfoByLoanId(final String reportname, final String loanId) {
		final String companyId=(String)ht.execute(new HibernateCallback<Object>() {
			public Object doInHibernate(Session session)
			throws HibernateException, SQLException {
				Query query=session.createQuery("select l.company.companyId from Loan l where l.loanId=:loanId");
				query.setParameter("loanId", loanId);
				return query.uniqueResult();
			}
		});
		return ht.execute(new HibernateCallback<Object>() {
			public Object doInHibernate(Session session)
			throws HibernateException, SQLException {
				Query query=session.createQuery("from "+reportname+" temp where temp.company.companyId=:companyId");
				query.setParameter("companyId", companyId);
				return query.uniqueResult();
			}
		});
	}

	/**
	 * 根据贷款编号查询贷款信息
	 * @param loanId 贷款编号
	 * @return Loan实体类对象
	 */
	public Object findLoanInfoByLoanId(final String loanId) {
		return ht.execute(new HibernateCallback<Object>() {
			public Object doInHibernate(Session session)
			throws HibernateException, SQLException {
				Query query=session.createQuery("from Loan l where l.loanId=:loanId");
				query.setParameter("loanId", loanId);
				return query.uniqueResult();
			}
		});
	}

	/**
	 * 更新实体类通用方法
	 * @param entity 实体类对象
	 */
	public void updateInfo(Object entity) {
		ht.update(entity);
	}
}
